package com.rr.middle;

import com.alibaba.fastjson.JSON;

/**
 * @author zhuangcanrun
 * @Date 2022/08/04/20:44
 * @description
 **/

/*
 * https://leetcode.cn/problems/rotate-image/
 * 数组题：旋转图像
 * */
public class Rotate {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        int startPoint = 0;
        int endPoint = n - 1;
        while (n >= 2) {
            doRotate(matrix, startPoint, endPoint);
            startPoint++;
            endPoint--;
            n -= 2;
        }
    }

    private void doRotate(int[][] matrix, int startPoint, int endPoint) {
        for (int i = 0; i < endPoint - startPoint; i++) {
            int temp = matrix[startPoint][startPoint + i];
            matrix[startPoint][startPoint + i] = matrix[endPoint - i][startPoint];
            matrix[endPoint - i][startPoint] = matrix[endPoint][endPoint - i];
            matrix[endPoint][endPoint - i] = matrix[startPoint + i][endPoint];
            matrix[startPoint + i][endPoint] = temp;
        }
    }

    public static void main(String[] args) {
        Rotate rotate = new Rotate();
        int[][] ints = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
//        int[][] ints = {{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}};
        rotate.rotate(ints);
        System.out.println(JSON.toJSONString(ints));
    }
}